//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.inteceptor;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

@Component
public class HandlerException implements HandlerExceptionResolver {
    protected Logger log = Logger.getLogger(this.getClass());

    public HandlerException() {
    }

    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object obj, Exception ex) {
        this.log.warn(obj == null ? "Null" : obj.toString(), ex);
        if (ex instanceof MaxUploadSizeExceededException) {
            this.outputResponse(response, "{ret:false, code:-9, msg:\"上传文件大小超过限制\"}");
            return null;
        } else if ("XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))) {
            this.outputResponse(response, "{ret:false, code:-9, msg:\"" + this.convStr(ex.getMessage()) + "\"}");
            return null;
        } else {
            ModelAndView mv = new ModelAndView("sys/alert");
            mv.addObject("msg", ex.getMessage());
            mv.addObject("_appPath", request.getContextPath());
            return mv;
        }
    }

    protected void outputResponse(HttpServletResponse response, String str) {
        response.setContentType("text/html; charset=utf-8");

        try {
            PrintWriter e = response.getWriter();
            e.print(str);
            e.close();
        } catch (IOException var4) {
            this.log.error(var4);
        }

    }

    protected String convStr(String str) {
        return str == null ? "" : str.replaceAll("\"", "\\\\\"").replaceAll("\n", "\\\\n").replaceAll("\r", "");
    }
}
